/*--------------------权限模块.用户管理--ly-----------------*/
/*--------------------（管理员权限）用户授权功能 user_jurisdiction.html------------------*/

var tableurl = jurisdictionport + "/RoleController/getRole"; //get请求数据url   
var Jurisdictionurl = jurisdictionport + "/UserRoleController/modifyByUserid"; //get请求数据url  
var ckeckedurl = jurisdictionport + "/UserRoleController/getRolesByUserid";

//页面table渲染
var tableget = (function() {
	$.get(tableurl, {
			'page': 1,
			'rows': 100
		},
		function(result) {
			var code = result.code;
			var list = result.object;
			if(code == 0) {
				var ant = list.total;
				antrows = ant; //将查询结果总数变成全局变量
				$('.totalNum').text(ant);
				$.each(list.list, function(idx, obj) {
					var idx = idx + 1; //排序默认从0开始，idx将作为显示题号，因此+1
					var id = obj.roleid;
					var Qhtml =
						'<tr id="' + obj.roleid + '">\
          <td>\
            <div class="layui-unselect layui-form-checkbox ' + obj.roleid + '" lay-skin="primary" data-id="' + obj.roleid + '">\
              <i class="layui-icon">&#xe605;</i>\
            </div>\
          </td>\
          <td>' + idx + '</td>\
          <td>' + obj.rolename + '</td>\
        </tr>';
					$("#user_role").append(Qhtml);

					var addckecked = (function() {
						//获取cookie:ueserid & Wtid
						var userid = $.cookie("userid");
						console.log("默认"+userid);

						$.get(ckeckedurl, {
							userid: userid
						}, function(result) {
							var code = result.code;
							var jurisdiction = result.object;
							if(code == 0) {
								$.each(jurisdiction, function(idx, obj) {
									var idx = idx + 1;
									//									var id = $(".layui-form-checkbox").attr("data-id");
									console.log(id, obj.roleid);
									if(id == obj.roleid) {
										$("." + id).addClass("layui-form-checked");
									}
								})
							}
						})
					});
					addckecked(id);
				});
			} else {
				alert('您的登录认证已失效，请重新登录！');
			};
			tableCheck.init();
		}, 'JSON');
});
tableget();

layui.use('laydate', function() {
	var laydate = layui.laydate;

	//执行一个laydate实例
	laydate.render({
		elem: '#start' //指定元素
	});

	//执行一个laydate实例
	laydate.render({
		elem: '#end' //指定元素
	});
});

//授权
function JurisdictionAll(argument) {
	//获取cookie:ueserid & Wtid
	var userid = $.cookie("userid");
	console.log("授权id"+userid);
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	var data = tableCheck.getData();
	var value = JSON.stringify(data);
	value = value.replace('[', '');
	value = value.replace(']', '');
	value = value.replace(/\"/g, ''); //去掉字符串中的英文引号
	layer.confirm('确定为该用户授予此权限?', function(index) {
		$.get(Jurisdictionurl, {
			'userid': userid,
			'roleids': value,
		}, function(result) {
			console.log(value, data);
			var code = result.code;
			if(code == 0) {
				layer.msg('授权成功!', {
					icon: 1,
					time: 1000
				}, function() {
					// 获得frame索引
					var index = parent.layer.getFrameIndex(window.name);
					//关闭当前frame
					parent.layer.close(index);
				});
			} else {
				layer.msg('授权失败，请稍后再试!', {
					icon: 2,
					time: 1000
				}, function() {
					//关闭当前frame
					//parent.layer.close(index);
				});
			}
		})
	});
}